export class MyMapOpt {

    static MapCoordSys={
        CJ02:"CJ02",
        GPS:"GPS"
    }
    static MapType={
        Marker:"Marker",
        Polyline:"Polyline",
        Polygon:"Polygon",
        Track:"Track"//todo 新增类型，跟踪Bug
    }

    static DialogType={
        AddData:"AddData",//添加数据
        EditData:"EditData",//编辑数据
        EditAndAddData:"EditAndAddData",//添加并编辑数据
    }
}

let BAIDU_LBS_TYPE = "bd09ll";
let pi = Math.PI;
let a = 6378245.0;
let ee = 0.00669342162296594323;

function transformLat(x, y) {
    let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
        + 0.2 * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
    return ret;
}
function transformLon(x, y) {
    let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
        * Math.sqrt(Math.abs(x));
    ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
        * pi)) * 2.0 / 3.0;
    return ret;
}

function outOfChina(lat, lon) {
    if (lon < 72.004 || lon > 137.8347)
        return true;
    if (lat < 0.8293 || lat > 55.8271)
        return true;
    return false;
}
/**
 *
 * @param {*} lat 纬度
 * @param {*} lon 经度
 * @returns
 */
function gps84_To_Gcj02(lon, lat) {
    if (outOfChina(lat, lon)) {
        return [lon,lat];
    }
    let dLat = transformLat(lon - 105.0, lat - 35.0);
    let dLon = transformLon(lon - 105.0, lat - 35.0);
    let radLat = lat / 180.0 * pi;
    let magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    let sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    let mgLat = lat + dLat;
    let mgLon = lon + dLon;
    return [mgLon, mgLat];
}
/**
 *
 * @param {*} lat 纬度
 * @param {*} lon 经度
 * @returns
 */
function gcj02_To_Gps84(lon, lat) {
    if (outOfChina(lat, lon)) {
        return [lon,lat];
    }
    let dLat = transformLat(lon - 105.0, lat - 35.0);
    let dLon = transformLon(lon - 105.0, lat - 35.0);
    let radLat = lat / 180.0 * pi;
    let magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    let sqrtMagic = Math.sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    let mgLat = lat + dLat;
    let mgLon = lon + dLon;

    let lontitude = lon * 2 - mgLon;
    let latitude = lat * 2 - mgLat;

    return [lontitude, latitude];
}
function convert(json,convertFunction){

    if (Array.isArray(json[0])) {
        const result = json.map((value,index,array)=>{
            return convert(value,convertFunction);
        })
        return result;
    }else{
        return convertFunction(json[0],json[1]);
    }
}

export function convert_gps84_To_Gcj02 (array){
    return convert(array,gps84_To_Gcj02);
}
export function convert_gcj02_To_Gps84 (array){
    return convert(array,gcj02_To_Gps84);
}
